package com.waiting.leetcode;

import java.util.*;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/14
 **/
public class Main {

    public void solution2(int k, String input) {
        List<String> output = new LinkedList<>();
        Map<Character, Integer> lookup = new HashMap<Character, Integer>();
        for (int i = 0; i + k < input.length(); i++) {
            String item = input.substring(i, i + k);
            int duplicateCount = 0;
            lookup.clear();
            for (char temp : item.toCharArray()) {
                lookup.put(temp, lookup.getOrDefault(temp, 0) + 1);
                if (lookup.getOrDefault(temp, 0) > 1) {
                    duplicateCount++;
                }
            }

            if (duplicateCount == 1) {
                output.add(item);
                i += k;
            }
        }
        for (String item : output) {
            System.out.print(item + " ");
        }
    }

    List function(String s, int k) {
        List result = new ArrayList();
        if (s == null || s.length() == 0 || s.length() < k)
            return result;

        int charCount[] = new int[26];
        int len = s.length();
        // tracks unique counts
        int count = 0;

        for (int i = 0; i < len; i++) {
            // if unique, inc count
            if (charCount[s.charAt(i) - 'a'] == 0)
                count++;
            // inc char count value in count tracker arr
            charCount[s.charAt(i) - 'a']++;
            // if i is min of k-1 unique chars
            if (i >= k - 1) {
                // holds the start index
                int startIndex = i - (k - 1);
                // if you've found k-1 unique chars, add to result
                if (count == k - 1)
                    result.add(s.substring(startIndex, i + 1));
                // remove the count of start index from arr
                charCount[s.charAt(startIndex) - 'a']--;
                // if start index element was unique, dec the count
                if (charCount[s.charAt(startIndex) - 'a'] == 0)
                    count--;
            }
        }
        return result;
    }

}
